package com.freight.common.core.web.controller;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.freight.common.core.constant.CacheConstants;
import com.freight.common.core.constant.Constants;
import com.freight.common.core.utils.ServletUtils;
import com.freight.common.core.web.domain.MyPlusEntity;

import java.util.Date;

/**
 * 基础服务实现类
 */
public class BaseServiceImpl<M extends BaseMapper<T>, T extends MyPlusEntity> extends ServiceImpl<M, T> implements BaseService<T> {
    @Override
    public boolean save(T entity) {
        entity.setCreateBy(ServletUtils.getRequest().getHeader(CacheConstants.DETAILS_USER_ID));
        entity.setCreateTime(new Date());
        entity.setDelFlag(Constants.DB_NOT_DELETED);
        return super.save(entity);
    }

    @Override
    public boolean updateById(T entity) {
        entity.setUpdateBy(ServletUtils.getRequest().getHeader(CacheConstants.DETAILS_USER_ID));
        entity.setUpdateTime(new Date());
        return super.updateById(entity);
    }

    /**
     * 新增前置操作
     * @param entity
     */
    @Override
    public void preAdd(T entity) {
        entity.setCreateBy(ServletUtils.getRequest().getHeader(CacheConstants.DETAILS_USER_ID));
        entity.setCreateTime(new Date());
        entity.setDelFlag(Constants.DB_NOT_DELETED);
    }

    /**
     * 更新前置操作
     * @param entity
     */
    @Override
    public void preUpdate(T entity) {
        entity.setUpdateBy(ServletUtils.getRequest().getHeader(CacheConstants.DETAILS_USER_ID));
        entity.setUpdateTime(new Date());
    }
}
